package com.example.seventh_home_work

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import android.widget.SeekBar
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.seventh_home_work.databinding.ActivityMainBinding
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity(),ServiceConnection{

    val test="test"

    var binder: MusicService.MusicBinder?=null

    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        if (ContextCompat.checkSelfPermission(this, "android.permission.READ_MEDIA_AUDIO")
            != PackageManager.PERMISSION_GRANTED) {
            // 请求 READ_MEDIA_AUDIO 权限
            ActivityCompat.requestPermissions(this, arrayOf("android.permission.READ_MEDIA_AUDIO"),0)
        }else{
            startMusicService()
        }

        binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(
                seekBar: SeekBar,
                progress :Int,
                fromUser :Boolean
            ) {
                if(fromUser){
                    binder?.currentPosition=progress
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })

    }
    fun startMusicService(){
        val intent = Intent(this,MusicService::class.java)
        intent.putExtra(MusicService.commond,1)
        startService(intent)
        bindService(intent,this, Context.BIND_AUTO_CREATE)
    }

    fun onPlay(v:View){
        val intent = Intent(this,MusicService::class.java)
        intent.putExtra(MusicService.commond,1)
        startService(intent)
    }
    fun onPause(v:View){
        val intent = Intent(this,MusicService::class.java)
        intent.putExtra(MusicService.commond,2)
        startService(intent)
    }
    fun onStop(v:View){
        val intent = Intent(this,MusicService::class.java)
        intent.putExtra(MusicService.commond,3)
        startService(intent)
    }
    fun onNext(v: View){
        val intent = Intent(this,MusicService::class.java)
        intent.putExtra(MusicService.commond,4)
        startService(intent)
    }
    fun onPre(v:View){
        val intent = Intent(this,MusicService::class.java)
        intent.putExtra(MusicService.commond,5)
        startService(intent)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onDestroy() {
        super.onDestroy()
        unbindService(this)
    }

    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        binder= service as MusicService.MusicBinder
        thread{
            while(binder!=null){
                Thread.sleep(1000)
                runOnUiThread{
                    binding.seekBar.max=binder!!.duration
                    binding.seekBar.progress = binder!!.currentPosition
                    binding.musicName.text=binder!!.musicName
                    binding.count.text="${binder!!.currentIndex+1}/${binder?.size}"
                }
            }
        }
    }

    override fun onServiceDisconnected(name: ComponentName?) {
        binder= null
    }
}